import {_decorator, Component, instantiate, Node, Prefab} from 'cc';
import Lattice from './Lattice';
import EventCenter from '../event/EventCenter';
import {GameEvent} from '../event/GameEvent';
import {Order} from '../models/Order';
import {BaseCard} from '../models/Card';
import {DEV} from 'cc/env';

const {ccclass, property} = _decorator;

@ccclass('UOrder')
export class UOrder extends Component{
    latticeList: Lattice[] = [];

    @property(Prefab)
    latticePrefab: Prefab = null;

    @property(Node)
    latticeNode: Node = null;

    @property(Node)
    adNode: Node = null;


    uid: number;

    get isAdLocked() {
        return this.adNode.active;
    }

    set isAdLocked(value: boolean) {
        this.adNode.active = value;
    }

    private get isComplete() {
        for(let lattice of this.latticeList){
            if(!lattice.isAdded)
                return false;
        }
        return true;
    }

    init(order?: Order) {
        this.latticeNode.removeAllChildren();
        this.latticeList = [];
        if(!order)
            return;
        let latticeList = order.cards
        for(let i = 0; i < latticeList.length; i++){
            let latticeNode = instantiate(this.latticePrefab);
            let lattice = latticeNode.getComponent(Lattice);
            let latticeInfo = latticeList[i];
            lattice.init(latticeInfo,order.name);
            this.latticeList.push(lattice);
            this.latticeNode.addChild(latticeNode);
        }
    }

    async onAdClick() {
        console.log("看广告");
        
        
    }


    AddCard(card:BaseCard,index:number) {
        let lattice = this.latticeList[index];
        if (lattice) {
            lattice.addCard(card);
        }

        return this.isComplete;
    }
}